package ywpt.web.action.knowledge;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.KnowLib;
import ywpt.model.SysMsgInfo;
import ywpt.service.knowledge.KnowLibService;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class KnowAuditAction extends BaseAction implements ModelDriven<KnowLib>, Preparable{

	/**
	 * 知识审核
	 */
	private static final long serialVersionUID = -7535558737374489661L;
	private static final int DEFAULT_PAGE_SIZE = 100;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
	private Long id;
	private KnowLib knowLib;
	private List<KnowLib> knowLibList = new ArrayList<KnowLib>();
	private KnowLibService knowLibService;
	
	private SysMsgInfo sysMsgInfo;
	private List<SysMsgInfo> sysMsgInfoList = new ArrayList<SysMsgInfo>();
	private SysMsgInfoService sysMsgInfoService;
	private String msg;
	
	
	public SysMsgInfo getSysMsgInfo() {
		return sysMsgInfo;
	}
	public void setSysMsgInfo(SysMsgInfo sysMsgInfo) {
		this.sysMsgInfo = sysMsgInfo;
	}
	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}
	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public KnowLibService getKnowLibService() {
		return knowLibService;
	}
	public void setKnowLibService(KnowLibService knowLibService) {
		this.knowLibService = knowLibService;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public KnowLib getKnowLib() {
		return knowLib;
	}
	public void setKnowLib(KnowLib knowLib) {
		this.knowLib = knowLib;
	}
	public List<KnowLib> getKnowLibList() {
		return knowLibList;
	}
	public void setKnowLibList(List<KnowLib> knowLibList) {
		this.knowLibList = knowLibList;
	}
	
	public boolean[] getPopedom() {
		return popedom;
	}
	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	/*-------------------------------------------------------------------------------------------*/
	@Override
	public KnowLib getModel() {
		return this.knowLib;
	}
	@Override
	public void prepare() throws Exception {
		if(id!=null){
			 this.knowLib = this.knowLibService.getKnowLibById(id);
		}else{
			this.knowLib = new KnowLib();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.KNOW_AUDIT);
	}
	/*===========================================================================================*/
	public String list() throws Exception {
		this.knowLibList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(KnowLib.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.eq("checkFlag", false));
		query.add(Restrictions.eq("submitFlag", true));
		query.createAlias("checker", "checker").add(Restrictions.eq("checker.id", this.getCurrentUser().getPeople().getId()));
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= knowLibService.getKnowLibCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,
				DEFAULT_PAGE_SIZE,0);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		knowLibList.addAll(knowLibService.getKnowLibs(query, pageSize, rowStartEnd[0]));		
		return SUCCESS;
	}
	
	public String view() {
		this.knowLib = this.knowLibService.getKnowLibById(id);
		this.sysMsgInfoList.addAll(this.sysMsgInfoService.getMsgs(8, id));
		return INPUT;
	}	
	
	
	public String disSubmit()
	{
		if(id==null){
			this.addActionMessage(SystemMessage.NO_OBJECT);
		}else{
			knowLib=knowLibService.getKnowLibById(id);
			if (!knowLib.getCheckFlag()) {
				if(knowLib.getSubmitFlag())
				{
					knowLib.setSubmitFlag(false);
					knowLibService.updatKnowLib(knowLib,null);
					sysMsgInfoService.invalidMsg(8, id);
					addMsg(knowLib);
					this.addActionMessage(SystemMessage.DISSUMBIT_SUCCESS);
				}
				else
					this.addActionMessage(SystemMessage.DISSUMBIT__REPEAT);
			} 
			else  
				this.addActionMessage(SystemMessage.AUDITING_SAVE);
				
		}
		return SUCCESS;
	}
	
	
	
	
	public String check(){
		if(id==null){
			this.addActionMessage(SystemMessage.NO_OBJECT);
			
		}else{
			knowLib=knowLibService.getKnowLibById(id);
			if(!knowLib.getCheckFlag())
			{
				knowLib.setCheckFlag(true);
				knowLibService.updatKnowLib(knowLib,null);
				sysMsgInfoService.invalidMsg(8, id);
				this.addActionMessage(SystemMessage.AUDITING_SUCCESS);
			}
			else
				this.addActionMessage(SystemMessage.AUDITING_REPEAT);
		}
		return SUCCESS;
	}
	
	protected void addMsg(KnowLib knowLib) {
		
			sysMsgInfo = new SysMsgInfo();
			sysMsgInfo.setMsgTitle("知识退回!");
			if (!StringTools.isNullOrSpace(msg)) {
				sysMsgInfo.setMsgContent(this.getCurrentUser().getPeople().getPeopleName()+FORMAT.format(new Date())+":"+msg);
			}else {
				sysMsgInfo.setMsgContent(this.getCurrentUser().getPeople().getPeopleName()+"退回一份知识申请！");
			}
			sysMsgInfo.setReceiveUser(knowLib.getPeople().getId()+",");
			sysMsgInfo.setCreater(this.getCurrentUser());
			sysMsgInfo.setCreateDate(new Date());
			sysMsgInfo.setBusinessType(8);
			sysMsgInfo.setBusinessId(knowLib.getId());
			sysMsgInfo.setUrl("/know/self/edit-"+knowLib.getId()+".action");
			sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
		
		
	}
	public List<SysMsgInfo> getSysMsgInfoList() {
		return sysMsgInfoList;
	}
	public void setSysMsgInfoList(List<SysMsgInfo> sysMsgInfoList) {
		this.sysMsgInfoList = sysMsgInfoList;
	}
}
